﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

using DanielVaughan.UserOptionsModel;

namespace CalciumTemplateApp
{
	/// <summary>
	/// This view allows the user to set options 
	/// that are specified in the <see cref="AppSettings"/> class.
	/// </summary>
	public partial class OptionsView
	{
		public OptionsView()
		{
			InitializeComponent();
		}
	}

	/// <summary>
	/// This content control selects the template to use for displaying a user option.
	/// </summary>
	public class OptionTemplateSelector : ContentControl
	{
		protected override void OnContentChanged(object oldContent, object newContent)
		{
			DataTemplate template = GetDefaultContentTemplate();
			if (newContent != null)
			{
				IUserOptionReaderWriter writer = newContent as IUserOptionReaderWriter;
				if (writer != null)
				{
					string templateName = writer.UserOption.TemplateName;
					Debug.Assert(!string.IsNullOrWhiteSpace(templateName),
					             "A default template name should be supplied by the IUserOption.");
					template = (DataTemplate)Resources[templateName];
				}
			}

			ContentTemplate = template;
		}

		protected virtual DataTemplate GetDefaultContentTemplate()
		{
			return null;
		}
	}
}